<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>link start</h2>
          <h2 style="display: flex; gap: 8px">
            <span>{{ currentDate }}</span>
            <span>{{ currentTime }}</span>
            <span>{{ currentWeek }}</span>
          </h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import moment from "moment";
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref, toRefs, onBeforeUnmount } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElNotification } from "element-plus";
import useUserStore from "@/store/modules/user";

let useStore = useUserStore();
let loginForms = ref();
let $router = useRouter();
let $route = useRoute();
let loading = ref(false);
let loginForm = reactive({ username: "admin", password: "111111" });

const state = reactive({
  currentDate: "",
  currentTime: "",
  currentWeek: "",
  weekArr: ["天", "一", "二", "三", "四", "五", "六"],
  clock: null,
});
const { currentDate, currentTime, currentWeek } = toRefs(state);

const login = async () => {
  await loginForms.value.validate();
  loading.value = true;
  try {
    //保证登录成功
    await useStore.userLogin(loginForm);
    //编程式导航跳转到展示数据首页
    //判断登录的时候,路由路径当中是否有query参数，如果有就往query参数挑战，没有跳转到首页
    let redirect: any = $route.query.redirect;
    $router.push({ path: redirect || "/" });
    ElNotification({
      type: "success",
      message: "欢迎回来",
    });
    loading.value = false;
  } catch (error) {
    loading.value = false;
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
  }
};

//自定义校验规则函数
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback();
  } else {
    callback(new Error("密码长度至少六位"));
  }
};

//定义表单校验需要配置对象
const rules = {
  username: [
    {
      required: true,
      min: 5,
      max: 10,
      message: "账号长度至少五位",
      trigger: "change",
    },
  ],
  password: [{ trigger: "change", validator: validatorPassword }],
};

const getNowTime = () => {
  const date = moment().format("YYYY年MM月DD日");
  const time = moment().format("kk:mm:ss");
  const week = moment().day();
  state.currentTime = time + " 星期" + state.weekArr[week];
  state.currentDate = date;
  state.currentTime = time;
  state.currentWeek = " 星期" + state.weekArr[week];
};

const init = () => {
  getNowTime();
  (state.clock as any) = setInterval(() => {
    getNowTime();
  }, 1000);
};

init();

onBeforeUnmount(() => {
  state.clock = null;
});
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
